package pl.edu.agh.intobl.operators;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import org.uncommons.watchmaker.framework.EvolutionaryOperator;

import pl.edu.agh.intobl.RouteSet;

public class RouteSetInversion implements EvolutionaryOperator<RouteSet> {

	final int probability;
	
	public RouteSetInversion(double probability) {
		this.probability = (int) probability * 1000;
	}
	
	@Override
	public List<RouteSet> apply(final List<RouteSet> population, Random rand) {
		List<RouteSet> newPopulation = new LinkedList<RouteSet>();
		for (RouteSet rSet : population) {
			RouteSet clonedRouteSet = (RouteSet) rSet.clone();
			newPopulation.add(clonedRouteSet);
		}
		
		if (probability > rand.nextInt(1000)) {
			inverse(newPopulation, rand);
		}
		
		return newPopulation;
	}

	private void inverse(List<RouteSet> newPopulation, Random rand) {
		newPopulation.get(rand.nextInt(newPopulation.size())).inverseRandomSubRoute(rand);
	}
}
